<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class PenyesuaianObat extends zulController {
	function __construct(){
		parent::__construct();
		$this->load->model(array("PenyesuaianObatModel","HistoryHargaModel"));
		$this->checkLogin();
		$this->checkAkses(1);
	}
	public function index(){
		$colModel['NO'] = array('No', 20, TRUE, 'center', 0);
        $colModel['TGL_PENYESUAIAN'] = array('Tanggal Penyesuaian', 110, FALSE, 'center', 1);
		$colModel['KETERANGAN'] = array('Keterangan', 120, FALSE, 'center', 1);
		$colModel['JENIS'] = array('Jenis Penyesuaian', 130, FALSE, 'center', 0);
		$colModel['LIHAT_DETAIL'] = array('Lihat Detail', 70, FALSE, 'center', 0);
		$colModel['EDIT'] = array('Edit', 70, FALSE, 'center', 0);
        $gridParams = array(
            'width' => 'auto',
            'height' => '390',
            'rp' => 15,
            'rpOptions' => '[10,15,20,25,40]',
            'pagestat' => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Penyesuaian Obat'
        );
        $buttons[] = array('Tambah', 'add', 'PenyesuaianObat');
        // $buttons[] = array('Hapus', 'delete', 'PenyesuaianObat');
        $buttons[] = array('separator');
        $buttons[] = array('Pilih Semua', 'add', 'PenyesuaianObat');
        $buttons[] = array('Hapus Pilihan', 'delete', 'PenyesuaianObat');
        $buttons[] = array('separator');
		
        $grid_js = build_grid_js('flex1', base_url("index.php/PenyesuaianObat/gridPenyesuaianObat"), $colModel, 'ID_PENYESUAIAN', 'ASC', $gridParams, $buttons);

        $data['title'] = "Daftar Penyesuaian Obat";
        $data['info'] = "Daftar Penyesuaian Obat";
        $data['js_grid'] = $grid_js;


        $data['added_js'] =
                "<script type='text/javascript'>
			function PenyesuaianObat(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Hapus Pilihan')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Tambah')
				{
					location.href='" . site_url() . "/PenyesuaianObat/formPenyesuaianObat/'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin mengaktifkan ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" .site_url('/PenerimaanObat/deletePenyesuaianObat')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}
			} 
			</script>
			";
		$data["active"] = "transaksi";
		$data["content"]	= $this->load->view("grid",$data,true);
		$this->load->view("main_new",$data);
	}
	function gridPenyesuaianObat(){
		$valid_fields = array('ID_PENYESUAIAN','KETERANGAN', 'JENIS', 'TGL_PENYESUAIAN');
        $this->flexigrid->validate_post('ID_PENYESUAIAN', 'ASC', $valid_fields);
        $records = $this->PenyesuaianObatModel->gridPenyesuaianObat();
        $this->output->set_header($this->config->item('json_header'));
        $no = 1;
        foreach ($records['records']->result() as $row) {
            $record_items[] = array($row->ID_PENYESUAIAN,
                $no,
				$this->dateFormat($row->TGL_PENYESUAIAN),
				str_replace("<p>","</p>",$row->KETERANGAN),
				$row->JENIS == 1 ? "Penambahan" : "Pengurangan",
				"<a class='fancybox fancybox.ajax' href='".site_url("/PenyesuaianObat/viewDetail/".$row->ID_PENYESUAIAN)."'><img src='". base_url()."assets/images/grid/magnifier.png'></a>",
				"<a href=\"".site_url("/PenyesuaianObat/formPenyesuaianObat/".$row->ID_PENYESUAIAN)."\"><img src=\"". base_url()."assets/images/grid/edit.png\"></a>"
            );
            $no++;
        }
        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	function formPenyesuaianObat($parameter=FALSE){
		$this->load->library("form_validation");
		$this->load->model("ObatModel");
		$this->load->model("DetailPenyesuaianModel");
		$this->form_validation->set_error_delimiters('<div class="alert alert-error fade in"><button type="button" class="close" data-dismiss="alert">&times;</button>', '</div>');
		$this->form_validation->set_rules(array(
		array('field'=>'keterangan','label'=>'Keterangan','rules'=>'required'),
		array('field'=>'tgl_penyesuaian','label'=>'Tanggal Penyesuaian','rules'=>'required'),
		array('field'=>'jenis','label'=>'Jenis Penyesuaian','rules'=>'required'),
		));
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		if($this->form_validation->run() == FALSE){
			if($parameter == TRUE){
				$data["value"] 		= $this->PenyesuaianObatModel->get($parameter);
				$join				= array(
										array("table"=>"detail_penyesuaian","join_key"=>"ID_HISTORY_HARGA","join_table"=>"history_harga"),
										array("table"=>"history_harga","join_key"=>"ID_OBAT","join_table"=>"obat")
									);
				$data["getDetail"]	= $this->DetailPenyesuaianModel->get_join($join,$parameter);
			}
			$data["getObat"]	= $this->ObatModel->get(FALSE,FALSE);
			$data["parameter"]	= $parameter;
			$data["active"] = "transaksi";
			$data["content"]	= $this->load->view("formPenyesuaianObat",$data,true);
			$this->load->view("main_new",$data);
		} else {
			$keterangan	= $this->input->post("keterangan");
			$tanggal	= date("Y-m-d",strtotime($this->input->post("tgl_penyesuaian")));
			$jenis		= $this->input->post("jenis");
			$data	= array(
			"KETERANGAN"=>$keterangan,
			"TGL_PENYESUAIAN"=>$tanggal,
			"JENIS"=>$jenis
			);
			$id_penyesuaian	= $this->PenyesuaianObatModel->save($data, $parameter);
			$this->DetailPenyesuaianModel->delete($id_penyesuaian);
			$getObat		= $this->ObatModel->get(FALSE,FALSE);
			foreach($getObat as $row){
				$jumlah	= $this->input->post("jumlah".$row["ID_OBAT"]);
				$id_history	= $this->HistoryHargaModel->get_by(array("ID_OBAT"=>$row["ID_OBAT"]),FALSE,FALSE,TRUE);
				if($jumlah != NULL){
					$data	= array(
					"ID_PENYESUAIAN"=>$id_penyesuaian,
					"ID_HISTORY_HARGA"=>$id_history["ID_HISTORY_HARGA"],
					"JUMLAH"=>$jumlah
					);
					$this->DetailPenyesuaianModel->save($data);
				}
			}
			redirect("/PenyesuaianObat");
		}
		return $parameter;
	}
	function viewDetail($parameter){
		$this->load->model("DetailPenyesuaianModel");
		$join	= array(
					array("table"=>"detail_penyesuaian","join_key"=>"ID_HISTORY_HARGA","join_table"=>"history_harga"),
					array("table"=>"history_harga","join_key"=>"ID_OBAT","join_table"=>"obat")
				);
		$data["getDetail"]	= $this->DetailPenyesuaianModel->get_join($join,$parameter);
		$this->load->view("detailPenyesuaian",$data);
	}
	function deletePenerimaanObat(){
		$ids	= $this->input->post('items');
		$this->PenyesuaianObatModel->delete($ids);
		$msg ='Data yang anda pilih berhasil dihapus ';
		$this->output->set_output($msg);
	}
}